/**
 * @ClassName: ChangePerferDealerFragment
 * @Description: 选择经销商
 * @author Mengting Lu
 * @date 2015年8月6日 下午8:40:03
 */
package com.shanghaionstar.discover.dealer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.adapter.AroundDealerAdapter;
import com.shanghaionstar.discover.dealer.iview.IChangePreferDealerView;
import com.shanghaionstar.discover.dealer.presenter.ChangePreferDealerPresenter;
import com.shanghaionstar.user.adapter.CommonAdapter;
import com.shanghaionstar.user.adapter.OnstarFlowCommonAdapter;
import com.shanghaionstar.user.adapter.ViewHolder;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * @author Mengting Lu
 * @ClassName: ChangePerferDealerFragment
 * @Description: 选择经销商
 * @date 2015年8月6日 下午8:40:03
 */
public class ChangePreferDealerFragment extends BaseFragment implements OnClickListener, IChangePreferDealerView {
    @FindViewById
    private ProgressLayout pd_progress;
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private LinearLayout datas_layout;
    @FindViewById
    private TextView current_city, tv_more;
    @FindViewById(R.id.tv_no_datas)
    private TextView no_datas;
    @FindViewById(R.id.iv_arrow)
    private ImageView arrowIv;
    @FindViewById
    private ListView around_dealer_list;
    @FindViewById
    private RelativeLayout change_city, more_layout;
    @FindViewById
    private ProgressBar more_progress;
    @FindViewById(R.id.content_va)
    private ViewAnimator contentVa;
    @FindViewById(R.id.left_lv)
    private ListView leftLv;
    @FindViewById(R.id.right_lv)
    private ListView rightLv;
    @FindViewById(R.id.line_v)
    private View lineV;
    @FindViewById
    private RelativeLayout layout_background;


    private BaseActivity parent;
    private AroundDealerAdapter aroundDealerAdapter;
    private boolean isUp = false;
    private ChangePreferDealerPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.discover_change_perfer_dealer;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        parent = (BaseActivity) activity;
    }

    @OnClick({R.id.more_layout, R.id.change_city})
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.more_layout) {
            tv_more.setVisibility(View.GONE);
            more_progress.setVisibility(View.VISIBLE);
            presenter.moreClick();
        } else if (v.getId() == R.id.change_city) {
            showSelectCityDialog();
            if (!isUp) {
                arrowIv.setImageResource(R.drawable.gray_arrow_up);
            } else {
                arrowIv.setImageResource(R.drawable.gray_arrow_down);
            }
            isUp = !isUp;
        }

    }


    // 显示选择城市的对话框
    private void showSelectCityDialog() {
        lineV.setVisibility(View.VISIBLE);
        contentVa.setDisplayedChild(1);
        presenter.getProvince();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (isFirstLoad()) {
            presenter = new ChangePreferDealerPresenter(parent.getScheduler());
            presenter.setView(this);
            presenter.getCoordinate();
        }

        around_dealer_list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                aroundDealerAdapter.setSelectItem(position);
                aroundDealerAdapter.notifyDataSetChanged();
                presenter.onDealerItemClick(position);
                back();
            }
        });
        change_city.performClick();
    }

    private void initView() {
        change_city.setOnClickListener(this);
        btnBackClick(top_layout);
        no_datas.setVisibility(View.VISIBLE);
        around_dealer_list.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        top_layout = null;
    }

    @Override
    public void setLoading(boolean isLoading) {
        pd_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(parent, e);
        datas_layout.setVisibility(View.GONE);
    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return parent;
    }

    @Override
    public void setupProvinceAdapter(List provinces) {
        leftLv.setAdapter(new OnstarFlowCommonAdapter<String>(getActivity(), provinces, R.layout.discover_select_city_item) {

            @Override
            public void convert(ViewHolder holder, String t, int pos) {
                holder.setText(R.id.title_tv, t);
                if (getClickPosition() == pos) {
                    holder.setBackgroundColor(R.id.city_item,
                            getActivity().getResources().getColor(R.color.white));
                } else {
                    holder.setBackgroundColor(R.id.city_item,
                            getActivity().getResources().getColor(R.color.color_f5f5f9));
                }
            }

        });

        leftLv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ((OnstarFlowCommonAdapter) leftLv.getAdapter()).setClickPosition(position);
                ((CommonAdapter) leftLv.getAdapter()).notifyDataSetChanged();
                presenter.onLeftItemClick(position);
            }
        });
    }

    @Override
    public void setupCityAdapter(List cityList) {
        if (rightLv.getAdapter() == null) {
            rightLv.setAdapter(new OnstarFlowCommonAdapter<String>(getActivity(), cityList, R.layout.discover_select_city_item_right) {

                @Override
                public void convert(ViewHolder holder, String t, int pos) {
                    holder.setText(R.id.title_tv, t);
                    if (getClickPosition() == pos) {
                        holder.setVisible(R.id.select_iv, true);
                    } else {
                        holder.setVisible(R.id.select_iv, false);
                    }
                }
            });
        } else {
            ((OnstarFlowCommonAdapter) rightLv.getAdapter()).setClickPosition(-1);
            ((CommonAdapter) rightLv.getAdapter()).notifyDataSetChanged();
        }

        rightLv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ((OnstarFlowCommonAdapter) rightLv.getAdapter()).setClickPosition(position);
                ((CommonAdapter) rightLv.getAdapter()).notifyDataSetChanged();
                presenter.onRightItemClick(position);
            }
        });
    }

    @Override
    public void onRightItemClick() {
        contentVa.setDisplayedChild(0);
        lineV.setVisibility(View.GONE);
        if (!isUp) {
            arrowIv.setImageResource(R.drawable.gray_arrow_up);
        } else {
            arrowIv.setImageResource(R.drawable.gray_arrow_down);
        }
        isUp = !isUp;    // TODO Auto-generated method stub
    }

    @Override
    public void notifyDataChange() {
        if(aroundDealerAdapter != null)
            aroundDealerAdapter.notifyDataSetChanged();
    }

    @Override
    public void showMore(boolean isHasMoreDealer) {
        if (isHasMoreDealer) {
            more_layout.setVisibility(View.VISIBLE);
            tv_more.setVisibility(View.VISIBLE);
            more_progress.setVisibility(View.GONE);
        } else {
            more_layout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageInit(List aroundDealer, String cityName) {
        no_datas.setVisibility(View.GONE);
        around_dealer_list.setVisibility(View.VISIBLE);
        current_city.setText(cityName);
        if (aroundDealer != null) {
            aroundDealerAdapter = new AroundDealerAdapter(aroundDealer,parent);
            around_dealer_list.setAdapter(aroundDealerAdapter);
        }
    }
}
